<?php

class MobileOrderController extends MobileController {

    static $model_name = 'order';
    static $display_name = '我的订单';
	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/

    /**
     * 获取我的订单列表
     */
    public function getIndex(){

        $title = '我的订单';
        $input['member_id'] = $this->member->id;
        $input['order'] = 'updated_at';
        $result =  $this->getService()->getList($input);
        $entries = $result['entries'];
        $links = $result['links'];

        return View::make(self::$view_path.'.order.list', compact('entries', 'links', 'title'));
    }

    /**
     * 下单
     */
    public function doCreate(){
        $foodIds = Input::get('food_ids');
        $counts = Input::get('counts');
        $totalPrice = Input::get('total_price');

        $order = $this->getService()->createOrder($this->member, $foodIds, $counts, $totalPrice);
        if(!$order){
            return Redirect::action('MobileBasketController@getIndex')->with('errors',$this->getService()->errors());
        }
        return Redirect::action('MobileOrderController@getDetail', $order->id);
    }

    /**
     * 使用积分结算订单
     * @param $order
     * @return mixed
     */
    public function doSettleByIntegral($order){
        $orderService = new OrderService($order);
        //使用积分结算
        if(!$orderService->settle(MemberAccount::INTEGRAL_ACCOUNT)){
            return Redirect::action('MobileOrderController@getDetail', $order->id)->with('errors',$orderService->errors());
        }
        return Redirect::action('MobileOrderController@getDetail', $order->id);
    }

    /**
     * 订单作废
     * @param $order
     */
    public function doDelete($order){
        $result = $this->getService()->deleteOrder($order);
        if(!$result){
            return Redirect::action('MobileOrderController@getIndex')->with('errors',$this->getService()->errors());
        }
        return Redirect::action('MobileOrderController@getIndex');
    }

    /**
     * 订单详情
     * @param $order
     */
    public function getDetail($order){
        //积分是否足够结算该订单
        $isIntegralEnough = bccomp($this->member->getAccount(MemberAccount::INTEGRAL_ACCOUNT)->current_money, $order->getIntegralPrice())>=0;
        return View::make(self::$view_path.'.order.detail', compact('order', 'isIntegralEnough'));
    }

    /**
     * 生成订单二维码
     * @param $order
     */
    public function getORCode($order){
        return View::make(self::$view_path.'.order.qr_code', compact('order'));
    }


    public function getService() {
        if(is_null($this->service)){
            $this->service = new MobileOrderService();
        }
        return $this->service;
    }

}
